package com.ruoyi.basebill.controller;

import com.ruoyi.basebill.DTO.*;
import com.ruoyi.basebill.domain.TWfzRemuneration;
import com.ruoyi.basebill.domain.WfzJtgzfl;
import com.ruoyi.basebill.domain.WfzXczbsb;
import com.ruoyi.basebill.service.IApiService;
import com.ruoyi.basebill.service.ITWfzRemunerationService;
import com.ruoyi.basebill.service.IWfzJtgzflService;
import com.ruoyi.basebill.service.IWfzXczbsbService;
import com.ruoyi.basedata.domain.WfzPerson;
import com.ruoyi.basedata.domain.WfzSbjs;
import com.ruoyi.basedata.service.IWfzPersonService;
import com.ruoyi.basedata.service.IWfzSbjsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @projectName: wfz
 * @package: com.ruoyi.basebill.controller
 * @className: Apicontroller
 * @author: Eric
 * @description: TODO 对接盖雅的接口
 * @date: 2023/1/6 10:23
 * @version: 1.0
 */
@Api("薪酬总表接口")
@Validated
@RestController
@RequestMapping("/api/xczb")
public class Apicontroller extends BaseController {
    @Autowired
    private ITWfzRemunerationService tWfzRemunerationService;

    @Autowired
    private IWfzXczbsbService wfzXczbsbService;

    @Autowired
    private IWfzPersonService wfzPersonService;
    @Autowired
    private IApiService IApiService;

    //计提工资福利接口
    @Autowired
    private IWfzJtgzflService jtgzflService;

    @Autowired
    private IWfzSbjsService wfzSbjsService;


    @ApiOperation("薪酬总表-工资发放")
    @Log(title = "薪酬总表—工资发放接口",businessType = BusinessType.INTERFACE_GZFF)
    @PostMapping("/gzff")
    public AjaxResult importRemuneration(@RequestBody @Valid RemunerationInfo remunerationInfo){

        AjaxResult ajax = AjaxResult.success();
        AjaxResult error = AjaxResult.error();

        StringBuffer stringBuffer = new StringBuffer();
        String fsourcebillid = remunerationInfo.getFsourcebillid();



        TWfzRemuneration tWfzRemuneration = new TWfzRemuneration();
        tWfzRemuneration.setFsourcebillid(fsourcebillid);

        List<TWfzRemuneration> tWfzRemunerations = tWfzRemunerationService.selectTWfzRemunerationList(tWfzRemuneration);

        for (TWfzRemuneration wfzRemuneration : tWfzRemunerations) {
            String fstate = wfzRemuneration.getFstate();
            if(!"REVOKE".equals(fstate)){ //重复推送时，该单据需为作废状态
                stringBuffer.append("该单据为在数据中台有后续业务，不允许重复推送！");
                error.put("msg",stringBuffer.toString());
                error.put("state","0");
                return error;
            }
            }



        if(stringBuffer!=null&&!"".equals(stringBuffer.toString())){
            error.put("msg","该单据为在数据中台有后续业务，不允许重复推送！");
            error.put("state","0");
            return error;
        }



//        if(stringBuffer!=null){
//            error.put("msg","该单据为在数据中台有后续业务，不允许重复推送！");
//            error.put("state","0");
//            return error;
//        }

        tWfzRemuneration.setFcreator(getUsername());
        tWfzRemuneration.setFcreatetime(new Date());
        tWfzRemuneration.setFlastupdateuser(getUsername());
        tWfzRemuneration.setFlastupdatetime(new Date());
        tWfzRemuneration.setFstate("SAVE");
        int i = IApiService.importRemuneration(remunerationInfo, tWfzRemuneration, error);
         if (i == 1){
            ajax.put("msg","保存成功");
            ajax.put("state","1");
        } /*else if (i == -1){
            return error;
        } */else {
            return error;
        }

        return  ajax;

    }

    @ApiOperation("员工档案")
    @Log(title = "员工档案",businessType = BusinessType.INTERFACE_GZFF)
    @PostMapping("/ygda")
    public AjaxResult importYgda(@RequestBody @Valid YgdaInfo ygdaInfo){

        AjaxResult ajax = AjaxResult.success();
        AjaxResult error = AjaxResult.error();

        StringBuffer stringBuffer = new StringBuffer();




        WfzPerson wfzPerson = new WfzPerson();
        wfzPerson.setFnumber(ygdaInfo.getFnumber());

        List<WfzPerson> wfzPersons = wfzPersonService.selectWfzPersonList(wfzPerson);

        if(wfzPersons.size()>0){
            for (WfzPerson wfzPerson1 : wfzPersons) {
            String fstate = wfzPerson1.getFstate();
            if(!"REVOKE".equals(fstate)){ //重复推送时，该单据需为作废状态
                stringBuffer.append("该单据为在数据中台有后续业务，不允许重复推送！");
                error.put("msg",stringBuffer.toString());
                error.put("state","0");
                return error;
            }
        }
//            if(stringBuffer != null){
//
//                return error;
//            }
        }
        wfzPerson.setFcreator(getUsername());
        wfzPerson.setFcreatetime(new Date());
        wfzPerson.setFlastupdateuser(getUsername());
        wfzPerson.setFlastupdatetime(new Date());
        wfzPerson.setFstate("SAVE");
        int i = IApiService.importYgda(ygdaInfo, wfzPerson);
        if (i==1){
            ajax.put("msg","保存成功");
            ajax.put("state","1");
        }else{
            ajax.put("msg","保存失败");
            ajax.put("state","0");
        }
        return  ajax;

    }

    @ApiOperation("薪酬总表-当月社保数据")
    @Log(title = "薪酬总表-当月社保数据接口",businessType = BusinessType.INTERFACE_GZFF)
    @PostMapping("/xczbsb")
    public AjaxResult importXczbsb(@RequestBody @Valid XczbsbInfo xczbsbInfo){
        AjaxResult ajax = AjaxResult.success();
        AjaxResult error = AjaxResult.error();

        StringBuffer stringBuffer = new StringBuffer();
        String fsourcebillid = xczbsbInfo.getFsourcebillid();


        WfzXczbsb wfzXczbsb = new WfzXczbsb();
        wfzXczbsb.setFsourcebillid(fsourcebillid);

        List<WfzXczbsb> wfzXczbsbs = wfzXczbsbService.selectWfzXczbsbList(wfzXczbsb);
        for (WfzXczbsb xczbsb : wfzXczbsbs) {
            String fstate = xczbsb.getFstate();
            if (!"REVOKE".equals(fstate)){ //重复推送时，该单据需为作废状态
                stringBuffer.append("该单据为在数据中台有后续业务，不允许重复推送！");

            }
        }
        if(stringBuffer.length() != 0){
            error.put("msg","该单据为在数据中台有后续业务，不允许重复推送！");
            error.put("state","0");
            return error;
        }

        wfzXczbsb.setFcreator(getUsername());
        wfzXczbsb.setFcreatetime(new Date());
        wfzXczbsb.setFlastupdateuser(getUsername());
        wfzXczbsb.setFlastupdatetime(new Date());
        wfzXczbsb.setFstate("SAVE");
        int i = IApiService.importXczbsb(xczbsbInfo, wfzXczbsb,error);
        if (i == 1){
            ajax.put("msg","保存成功");
            ajax.put("state","1");
        } /*else if (i == -1){
            return error;
        } */else {
//            error.put("msg","保存失败");
//            error.put("state","0");
            return error;
        }
        return ajax;
    }
    @ApiOperation("薪酬总表-计提工资福利")
    @Log(title = "薪酬总表-计提工资福利接口",businessType = BusinessType.INTERFACE_GZFF)
    @PostMapping("/jtgzfl")
    public AjaxResult importXczbsb(@RequestBody @Valid JtgzflInfo jtgzflInfo){
        AjaxResult ajax = AjaxResult.success();
        AjaxResult error = AjaxResult.error();

        StringBuffer stringBuffer = new StringBuffer();

        WfzJtgzfl wfzJtgzfl = new WfzJtgzfl();
        wfzJtgzfl.setFsourcebillid(jtgzflInfo.getFsourcebillid());

        List<WfzJtgzfl> wfzJtgzfls = jtgzflService.selectWfzJtgzflList(wfzJtgzfl);

        if (wfzJtgzfls.size() > 0) {
            for (WfzJtgzfl jtgzfl : wfzJtgzfls) {
                String fstate = jtgzfl.getFstate();
                if (!"REVOKE".equals(fstate)) {//重复推送，该单据需要为作废状态
                    stringBuffer.append("该单据为在数据中台有后续业务，不允许重复推送！");
                    error.put("msg", stringBuffer.toString());
                    error.put("state", "0");
                }
            }
        }
            wfzJtgzfl.setFcreator(getUsername());
            wfzJtgzfl.setFcreatetime(new Date());
            wfzJtgzfl.setFlastupdateuser(getUsername());
            wfzJtgzfl.setFlastupdatetime(new Date());
            wfzJtgzfl.setFstate("SAVE");
            int i = IApiService.importJtgzfl(jtgzflInfo, wfzJtgzfl, error);
            if (i == 1){
                ajax.put("msg","保存成功");
                ajax.put("state","1");
            } /*else if (i == -1){
                return error;
            } */else {
//                error.put("msg","保存失败");
//                error.put("state","0");
                return error;
            }
            return ajax;
        }


    @ApiOperation("社保基数")
    @Log(title = "社保基数",businessType = BusinessType.INTERFACE_GZFF)
    @PostMapping("/sbjs")
    public AjaxResult importSbjs(@RequestBody WfzSbjs wfzSbjs){
        AjaxResult error = AjaxResult.error();
        AjaxResult success = AjaxResult.success();
        String fpersonnumber = wfzSbjs.getFpersonnumber();

        WfzPerson person = new WfzPerson();
        person.setFnumber(fpersonnumber);

        List<WfzPerson> wfzPeople = wfzPersonService.selectWfzPersonList(person);

        if(wfzPeople.size()<0){
            error.put("msg","该员工"+fpersonnumber+"未同步到数据中台");
            return error;

        }
            wfzSbjs.setFcreator(getUsername());
            wfzSbjs.setFlastupdateuser(getUsername());
            wfzSbjsService.insertWfzSbjs(wfzSbjs);

        return success;


    }


}

